#include "RingQueue.hpp"
#include "sem.hpp"
#include "Mutex.hpp"
#include <iostream>

using namespace MutexModule;

template <class T>
void *Comsume(void *arg)
{
	RingQueue<T> *bq = static_cast<RingQueue<T> *>(arg);
	static Mutex mutex;
	while (true)
	{
		{
			MutexGuard guard(mutex);
			sleep(3);
			T data = 0;
			bq->Pop(data);

			std::cout << "消费了一个数据：" << data << std::endl;
		}
	}

	return nullptr;
}

template <class T>
void *Produce(void *arg)
{
	RingQueue<T> *bq = static_cast<RingQueue<T> *>(arg);
	int data = 1;
	static Mutex mutex;
	while (true)
	{
		{
			MutexGuard guard(mutex);
			sleep(2);
			bq->Equeue(data);
			std::cout << "生产了一个数据：" << data << std::endl;
			data++;
		}
	}

	return nullptr;
}

int main()
{
	RingQueue<int> *rq = new RingQueue<int>();
	pthread_t comsumer, producer;
	pthread_create(&comsumer, nullptr, Comsume<int>, rq);
	pthread_create(&producer, nullptr, Produce<int>, rq);

	pthread_join(comsumer, nullptr);
	pthread_join(producer, nullptr);

	delete rq;
	return 0;
}
